home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / HDX_BACK / HDX503 / ZERO.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-09  |  5.5 KB  |  205 lines

  1. /* zero.c */
  2.  
  3. /* 16-Mar-88  ml.    split up assist.c into this and markbad.c    */
  4. /* 11-Dec-87  ml.    added BSL concept to zero().            */
  5. /* 01-Nov-88  jye.  change add codes so that can be used for MS-DOS */
  6.  
  7.  
  8. #include "osbind.h"
  9. #include "obdefs.h"
  10. #include "mydefs.h"
  11. #include "part.h"
  12. #include "bsl.h"
  13. #include "addr.h"
  14.  
  15.  
  16. extern long gbslsiz();
  17. extern long bslsiz;
  18. extern BYTE *bsl;
  19. extern long ratio;
  20.  
  21.  
  22. /*           
  23.  * Zero logical dev
  24.  *
  25.  */
  26. zero(ldev)
  27. int ldev;
  28. {
  29.     UWORD pdev,ndirs, fatsiz;
  30.     UWORD bps, sects;
  31.     SECTOR fat0, data, size, dummy=0;
  32.     char bs[512];
  33.     char *newbs;
  34.     BOOT *boot;
  35.     int  bootsiz, i, ret;
  36.     char *buf1;
  37.     long remsect;
  38.     long datsect;
  39.     long datclst; 
  40.     long fatclst;
  41.     long entsect;
  42.     long nsect;
  43.     
  44.     if ((ret = rdsects(ldev, 1, bs, (SECTOR)0)) != 0) {
  45.         if (tsterr(ret) != OK)
  46.             err(bootread);
  47.         return ERROR;
  48.     }
  49.  
  50.     /*
  51.      * Zero file system's boot sector, FAT and DIR sectors.
  52.      */
  53.     boot = (BOOT *)bs;
  54.     gw((UWORD *)&boot->b_ndirs[0], &ndirs);
  55.     gw((UWORD *)&boot->b_spf[0], &fatsiz);
  56.     gw((UWORD *)&boot->b_bps[0], &bps);
  57.     gw((UWORD *)&boot->b_nsects[0], §s);
  58.     nsect = (long)sects;
  59.     ratio = (long)(bps / BPS);
  60.     size = (long)BPS * ratio;
  61.     if ((newbs = (char *)Malloc(size)) <= 0)    {
  62.         err(nomemory);
  63.         if (newbs > 0) Mfree((long)newbs);
  64.         return ERROR;
  65.     }
  66.     
  67.     if ((ret = zerosect(ldev, (SECTOR)0, 
  68.                     (1 + fatsiz*2 + ndirs*BPDIR/bps)*((UWORD)ratio))) != 0) {
  69.         if (tsterr(ret) != OK)
  70.             err(hdrwrite);
  71.         return ERROR;
  72.     }
  73.          
  74.  
  75.     /* Put boot information into new boot sector */
  76.     if ((ret = rdsects(ldev, (UWORD)ratio, newbs, (SECTOR)0)) != 0) {
  77.         if (tsterr(ret) != OK)
  78.             err(bootread);
  79.         return ERROR;
  80.     }
  81.     bootsiz = sizeof(BOOT);        
  82.     for (i = 0; i < bootsiz; i++)
  83.         newbs[i] = bs[i];
  84.     
  85.     /* put the last long word data into the new boot sector */
  86.     *(long *)&newbs[508] = *(long *)&bs[508];
  87.  
  88.     /* Write boot information back to disk */
  89.     if ((ret = wrsects(ldev, (UWORD)ratio, newbs, (SECTOR)0)) != 0) {
  90.         if (tsterr(ret) != OK)
  91.             err(bootwrit);
  92.         return ERROR;
  93.     }
  94.         
  95.  
  96.     /* # of sectors in datas:  
  97.      *                 datsect   = nsect - boot - 2*fatsiz - size of dir 
  98.      *                           = nsect-boot-2*fatsiz-(ndirs*BPDIR)/bps
  99.      * # of clusters in datas: 
  100.      *                datclst      = # of sectors in datas / SPC 
  101.      * # of clusters(entries) in Fat: 
  102.      *                fatclst   = fatsiz * (bps/2)
  103.      * # of sectors need in fat talbe for datclst:
  104.      *                entsect   = datclst / (bps/2)
  105.      * # of sectors need to remodify to 0xffff:
  106.      *                remsect   = fatsiz - entsect
  107.      */
  108.  
  109.     datsect = nsect - 1 - 2*fatsiz - (ndirs*BPDIR)/bps;
  110.     datclst = datsect / SPC;
  111.     fatclst = (long)fatsiz * (bps/2);
  112.     entsect = datclst / (bps/2);
  113.     remsect = ((long)fatsiz - entsect)*size;
  114.  
  115.  
  116.     /* # of sectors need to remodify to 0xffff */
  117.     if ((buf1 = (char *)Malloc(remsect)) <= 0)    {
  118.         err(nomemory);
  119.         if (newbs > 0) Mfree((long)newbs);
  120.         if (buf1 > 0) Mfree((long)buf1);
  121.         return ERROR;
  122.     }
  123.  
  124.     /* 
  125.      * Apr-23-93 jye: To fixed the write over to next partitiona in the Gemdos  
  126.      *                  , here just set those entries in Fat table, which don't
  127.      *                  correspond to the availble date sectors in the partition.
  128.      *                  The pointers should be:
  129.      *                      (boot+datclst/(bps/2))*ratio and 
  130.      *                    (boot+fatsiz+datclst/(bps/2))*ratio 
  131.      */
  132.     if ((ret = rdsects(ldev, (UWORD)(remsect/BPS), buf1, 
  133.                          (1+datclst/(bps/2))*ratio)) != OK) {
  134.         if (tsterr(ret) != OK)
  135.             err(bootwrit);
  136.         return ERROR;
  137.     }
  138.     /* fill the last sectors of FAT start at last entry with the 0xffff */
  139.     /* +2 is for the two reserved entries in the fat table */
  140.     fillfat(buf1, ((datclst % (bps/2))+2)*2, remsect, 0xffff);
  141.     if ((ret = wrsects(ldev,(UWORD)(remsect/BPS),buf1,
  142.                              (1+datclst/(bps/2))*ratio) != OK) ||
  143.         (ret = wrsects(ldev,(UWORD)(remsect/BPS),buf1,
  144.                              (1+fatsiz+datclst/(bps/2))*ratio) != OK)) {
  145.         if (tsterr(ret) != OK)
  146.             err(bootwrit);
  147.         return ERROR;
  148.     }
  149.     /* Apr-23-93 jye: Add above codes to fix the Gemdos over write next
  150.      *                   partition.
  151.      */
  152.  
  153.              
  154.  
  155.     /*
  156.      * Make first 2 entries in FATs more IBM-like.
  157.      */
  158.     if ((ret = rdsects(ldev,(UWORD)ratio,newbs,ratio)) != 0) {/* read FAT 0 */
  159.         return ERROR;
  160.     }
  161.     *(UWORD *)&newbs[0] = 0xf8ff;
  162.     *(UWORD *)&newbs[2] = 0xffff;
  163.     
  164.     /* write FAT 0 and FAT 1 */
  165.     if ((ret = wrsects(ldev, (UWORD)ratio, newbs, ratio)) != 0
  166.     || (ret = wrsects(ldev, (UWORD)ratio, newbs, 
  167.                 (SECTOR)((1+fatsiz)*ratio))) != 0) {
  168.     if (tsterr(ret) != OK)
  169.         err(fatwrite);
  170.     return ERROR;
  171.     }
  172.         
  173.     pdev = ldev;
  174.     log2phys(&pdev, &dummy);
  175.     
  176.     /* Get size of BSL */
  177.     if ((bslsiz = gbslsiz(pdev)) > 0L) {
  178.         /* Allocate memory for existing BSL */
  179.         if ((bsl = (BYTE *)mymalloc((int)bslsiz << 9)) <= 0)
  180.             return err(nomemory);
  181.             
  182.         /* Read in BSL */
  183.         if ((dummy = rdbsl(pdev)) != OK) {
  184.             free(bsl);
  185.             if (dummy = INVALID)
  186.                 err(cruptbsl);
  187.             return ERROR;
  188.         }
  189.         
  190.     fat0 = ratio;
  191.     data = (SECTOR)1 + (SECTOR)fatsiz*2 + (SECTOR)(ndirs*BPDIR/bps);
  192.     
  193.         /* Mark Vendor or User Bad Sectors into FAT */
  194.     if (bsl2fat(ldev, fat0, fatsiz*((UWORD)ratio), data*ratio, MEDIA) != OK) {
  195.             free(bsl);
  196.             return ERROR;
  197.         }
  198.     free(bsl);
  199.     }
  200.     if (newbs > 0)    Mfree((long)newbs);
  201.     if (buf1 > 0) Mfree((long)buf1);
  202.     return OK;
  203. }
  204.  
  205.